const vscode = require("vscode");

function activate(context) {
    let hello = vscode.commands.registerCommand("validate", () => {
        vscode.window.showInformationMessage("it works");
    });
    
    let lowerCase = vscode.commands.registerCommand("Lower", toLowerCase);
    let upperCase = vscode.commands.registerCommand("Upper", toUpperCase);
    let disposable = vscode.commands.registerCommand('webview', showPanelView);
    
    context.subscriptions.push(hello);
    context.subscriptions.push(lowerCase);
    context.subscriptions.push(upperCase);
    context.subscriptions.push(disposable)
}

function toLowerCase() {
    toLowerCaseOrUpperCase('toLowerCase');
}

function toUpperCase() {
    toLowerCaseOrUpperCase('toUpperCase');
}

/* command: 选中文本大小写转换 */
function toLowerCaseOrUpperCase(command) {
    const editor = vscode.window.activeTextEditor;
    if (editor) {
        const document = editor.document;
        const selection = editor.selection;
        const word = document.getText(selection);
        
        // 根据命令转换文本
        const newWord = command === 'toLowerCase' ? word.toLowerCase() : word.toUpperCase();
        
        // 替换选中的文本
        editor.edit(editorBuilder => {
            editorBuilder.replace(selection, newWord);
        });
    }
}

/* command: 显示窗口 */
function showPanelView () {
    const panel = vscode.window.createWebviewPanel(
        'testWebview', // viewType
        'WebView演示', // 视图标题
        vscode.ViewColumn.One, // 显示在编辑器的哪个部位
        {
          enableScripts: true, // 启用JS，默认禁用
          retainContextWhenHidden: true, // webview被隐藏时保持状态，避免被重置
        }
      );
      panel.webview.html = `<html><body>你好，我是Webview</body></html>`;
}



// this method is called when your extension is deactivated
function deactivate() {}

module.exports = {
    activate,
    deactivate,
};
